<?php
/**
 * [weliam] Copyright (c) 2016/3/23
 * coupon.ctrl
 * 手机端核销控制器
 */
defined('IN_IA') or exit('Access Denied');
wl_load()->model('activity');
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$uniacid = $_W['uniacid'];
$openid = $_W['openid'];
$sql_saler = "select * from " . tablename('tg_saler') . " where uniacid = :uniacid and openid = :openid and status = 1 ";
$parms = array(':uniacid' => $uniacid, 'openid' => $_W['openid']);
$saler = pdo_fetch($sql_saler, $parms);

if ($_W['uniacid'] == 4){
    $uniacid = 6;
}

if (empty($saler)) {
    $tip = '您不是核销员，无权访问此页面！';
    echo "<script>alert('" . $tip . "');location.href='" . app_url('goods/list') . "';</script>";
}

if ($op == 'display') {

    include wl_template('child');
}

if ($op == 'item') {

    $size = !empty($_GPC['pagesize']) ? $_GPC['pagesize'] : 10;
    $page = !empty($_GPC['page']) ? $_GPC['page'] : 1;
    $member = '';
    $status = '';
    $bb = '';

    $goods = pdo_fetchall("select goods_id from " . tablename('tg_openid_xiangmu') . " where uniacid = '{$uniacid}' and openid = '{$_GPC['openid']}' ");
    if (!empty($goods)) {
        $bb .= " and sid in ( ";
        for ($i = 0; $i < count($goods) - 1; $i++) {
            $bb .= intval($goods[$i]['goods_id']) . " , ";
        }
        $bb .= intval($goods[$i]['goods_id']) . " ) ";
    }
    if (!empty($_GPC['member'])) {
        $member = trim($_GPC['member']);
        $bb .= " and ( mobile LIKE '%{$member}%' or id_card LIKE '%{$member}%' ) ";
    }

    if (empty($_GPC['member'])) {
        $bb .= " and orderno in ( select orderno from " . tablename('tg_order') . " where uniacid = '{$uniacid}' and status = 8 ) ";
    } else {
        $bb .= " and check_status <> 1 and active_times > 0 and orderno <> '0' ";
    }

    $limit = " LIMIT " . ($page - 1) * $size . " , " . $size;

    $sql = "select * from " . tablename('tg_collect') . " where uniacid = '{$uniacid}' {$bb} order by id desc";
    $sql .= $limit;
    $total = pdo_fetchcolumn("select COUNT(*) from " . tablename('tg_collect') . " where uniacid = '{$uniacid}' {$bb}");
    $list = pdo_fetchall($sql);
    $pager = pagination($total, $page, $size);
//    $list = pdo_fetchall("select * from " .tablename('tg_collect') ." where uniacid = '{$uniacid}' and orderno in ( select orderno from "
//        .tablename('tg_order') ." where uniacid = '{$uniacid}' and status = 8 ) ");

    die(json_encode(array('list' => $list, 'total' => $total)));
}

if ($op == 'coupon') {

    $size = !empty($_GPC['pagesize']) ? $_GPC['pagesize'] : 10;
    $page = !empty($_GPC['page']) ? $_GPC['page'] : 1;
    $member = '';
    $bb = '';

    if (!empty($_GPC['member'])) {
        $member = trim($_GPC['member']);
        $key = " and ( mobile like '%{$member}%' or idcard like '%{$member}%' )";
        $bb .= ' and openid in (' . 'select from_user from ' . tablename('tg_member') . " where uniacid = {$uniacid}  {$key}" . ')';
    }
    $limit = " LIMIT " . ($page - 1) * $size . " , " . $size;

    $sql = "select * from " . tablename('tg_favorabler') . "  where uniacid = '{$uniacid}' and status = 0 {$bb} ";
    $sql .= " order by id " . $limit;

    $list = pdo_fetchall($sql);
    $total = pdo_fetchcolumn("select COUNT(*) from " . tablename('tg_favorabler') . " WHERE uniacid = {$uniacid} and status = 0 {$bb}");

    foreach ($list as &$value) {
        $member = pdo_fetch("select * from " . tablename('tg_member') . " where openid = :openid", array(':openid' => $value['openid']));
        $coupon = pdo_fetch("select name from " . tablename('tg_favorable') . " where id = :id", array(':id' => $value['favorable_id']));
        $value['user'] = $member;
        $value['mobile'] = $member['mobile'];
        $value['idcard'] = $member['idcard'];
        $value['coupon'] = $coupon;
    }

    $pager = pagination($total, $page, $size);
    die(json_encode(array('list' => $list, 'total' => $total)));

//    include wl_template('child/item');

}
//卡券发奖
if ($op == 'prize') {

    $size = !empty($_GPC['pagesize']) ? $_GPC['pagesize'] : 10;
    $page = !empty($_GPC['page']) ? $_GPC['page'] : 1;
    $member = '';
    $bb = '';

    if (!empty($_GPC['member'])) {
        $member = trim($_GPC['member']);
        $bb .= " and ( mobile like '%{$member}%' or idcard like '%{$member}%' )";
//        $bb .= ' and openid in (' . 'select from_user from ' . tablename('tg_member') . " where uniacid = {$uniacid}  {$key}" . ')';
    }
    $orderby = " ORDER BY favorable_times-prize_times DESC ";
    $groupby = " group by idcard ";
    $limit = " LIMIT " . ($page - 1) * $size . " , " . $size;

    $sql = "select uniacid,from_user,idcard,mobile,favorable_times,order_times,nickname,realname,prize_times from " . tablename('tg_member') . "  where uniacid = '{$uniacid}' and favorable_times > 0 {$bb} ";
    $sql .= $orderby . $limit;

    $list = pdo_fetchall($sql);
    $total = pdo_fetchcolumn("select COUNT(id) from " . tablename('tg_member') . "  where uniacid = '{$uniacid}' and favorable_times > 0 {$bb} ");

    foreach ($list as &$value) {
        $value['openid'] = $value['from_user'];
        if (empty($value['realname'])){
            $value['realname'] = $value['nickname'];
        }
        unset($value);
    }

    $pager = pagination($total, $page, $size);
    die(json_encode(array('list' => $list, 'total' => $total)));

//    include wl_template('child/item');

}

//发奖
if ($op == 'check_prize') {

    $idcard = $_GPC['member'];
    $times = $_GPC['times'];
    if (empty($times)) {
        $times = 10;
    }

    $i = 0;
    foreach ($idcard as &$value) {
        $member = pdo_fetchall("select * from " . tablename('tg_member') . " where uniacid = '{$uniacid}' and idcard = '{$value}' ");
        $favorable_times = 0;
        $prize_times = 0;
        $user = '';
        foreach ($member as &$item) {
            $favorable_times += $item['favorable_times'];
            $prize_times += $item['prize_times'];
            $user = $item['from_user'];
        }
        if ($favorable_times - $prize_times < $times) {
            break;
        }
        $data = array(
            'uniacid' => $uniacid,
            'idcard' => $value,
            'prize_user' => $openid,
            'prize_time' => TIMESTAMP,
            'prize_times' => $times
        );
        pdo_update('tg_member', array('prize_times +=' => $times), array('uniacid' => $uniacid, 'openid' => $user));
        pdo_insert('tg_prize', $data);
        $i++;
    }
    if ($i == count($idcard)) {
        die(json_encode(array('status' => 1, 'result' => "发奖成功！")));
    } elseif ($i == 0) {
        die(json_encode(array('status' => 0, 'result' => count($idcard) . "个由于卡券使用次数不足10次，无法发奖！")));
    } else {
        die(json_encode(array('status' => 0, 'result' => $i . '个发奖成功！' . "其中" . (count($idcard) - $i) . "个由于卡券使用次数不足10次，无法发奖！")));
    }

}

if ($op == 'check_coupon') {

    $ids = $_GPC['member'];
    $openid = $_GPC['openid'];

    foreach ($ids as &$value) {
        $checkorder = pdo_fetch("select * from" . tablename('tg_favorabler') . " where  id = '{$value}' ");

        $sql = "SELECT * FROM " . tablename('tg_favorable') . " where id = :id";
        $list = pdo_fetch($sql, array(':id' => $checkorder['favorable_id']));
        $is_hexiao_member = 1;

        if ($checkorder['times'] < 1) {
            die(json_encode(array('status' => 0, 'result' => '该卡券核销次数已用完，请勿重复核销！')));
//            message("该卡券核销次数已用完，请勿重复核销！", referer(), "error");
        }

        pdo_update('tg_favorabler', array('times' => ($checkorder['times'] - 1)), array('id' => $value));

        $data = array();
        $data['uniacid'] = $checkorder['uniacid'];
        $data['favorabler_id'] = $checkorder['id'];
        $data['favorable_id'] = $checkorder['favorable_id'];
        $data['openid'] = $checkorder['openid'];
        $data['check_user'] = $_W['user']['username'];
        $data['check_time'] = TIMESTAMP;
        pdo_insert('tg_hexiao', $data);
        if ($checkorder['times'] == 1) {
            pdo_update('tg_favorabler', array('status' => 1), array('id' => $value));
        }
        pdo_update('tg_member', array('favorable_times +=' => 1), array('uniacid' => $checkorder['uniacid'], 'openid' => $checkorder['openid']));
    }
    die(json_encode(array('status' => 1, 'result' => '核销成功！')));

}

if ($op == 'check_order') {
    $ordernos = $_GPC['member'];
    $openid = $_GPC['openid'];

    foreach ($ordernos as &$orderno) {
        $checkorder = pdo_fetch("select * from " . tablename('tg_collect') . " where  orderno = '{$orderno}' ");
        $is_hexiao_member = 1;
        $order = pdo_fetch('select * from ' . tablename('tg_order') . ' where orderno = :orderno', array(':orderno' => $checkorder['orderno']));

        if ($checkorder['active_times'] < 1) {
            die(json_encode(array('status' => 0, 'result' => '该项目已无服务次数，请勿重复服务！')));
        }
        pdo_update('tg_collect', array('active_times' => ($checkorder['active_times'] - 1)), array('id' => $checkorder['id']));
        $data = array();
        $data['uniacid'] = $checkorder['uniacid'];
        $data['cid'] = $checkorder['id'];
        $data['orderno'] = $checkorder['orderno'];
        $data['openid'] = $checkorder['openid'];
        $data['check_user'] = $_W['user']['username'];
        $data['check_time'] = TIMESTAMP;
        pdo_insert('tg_hexiao', $data);
        if ($checkorder['active_times'] == 1) {
            pdo_update('tg_collect', array('check_status' => 1), array('id' => $checkorder['id']));
        }

        $check_order = pdo_fetchall("select * from " . tablename('tg_collect') . " where  orderno = :orderno  and check_status = 0", array(':orderno' => $checkorder['orderno']));
        if (count($check_order) == 0) {
            pdo_update('tg_order', array('status' => 3, 'successtime' => TIMESTAMP), array('orderno' => $checkorder['orderno']));
        }
        pdo_update('tg_member', array('order_times +=' => 1), array('uniacid' => $checkorder['uniacid'], 'openid' => $checkorder['openid']));
    }
    die(json_encode(array('status' => 1, 'result' => '核销成功！')));
}
exit();